package com.wx.mall.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.vo.goods.SkuVo;
import com.wx.mall.common.vo.order.OrderItemReqVo;

import com.wx.mall.feign.GoodListFeignClient;
import com.wx.mall.order.entity.Order;
import com.wx.mall.order.entity.OrderItem;
import com.wx.mall.order.mapper.OrderItemMapper;
import com.wx.mall.order.service.IOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.order.service.IOrderService;
import com.wx.mall.order.vo.NewOrderItemVo;
import com.wx.mall.order.vo.OrderItemPrintReqVo;
import com.wx.mall.order.vo.OrderItemRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单项 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-04-13
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    @Autowired
   GoodListFeignClient skuData;
    @Autowired
    IOrderService service;


    @Override
    public List<OrderItem> getListId(Long orderId) {
        QueryWrapper<OrderItem> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        List<OrderItem> list=this.list(wrapper);
        return list;
    };

    @Override
    public List<OrderItemPrintReqVo> getByIdList(OrderItemReqVo ids) {

        List<OrderItemPrintReqVo> lists=new ArrayList<>();

        for (Long id:ids.getIds()
             ) {

            OrderItemPrintReqVo all=new OrderItemPrintReqVo();

            QueryWrapper<OrderItem> wrapper=new QueryWrapper<>();
            wrapper.eq("order_id",id);
            List<OrderItem> list=this.list(wrapper);
    List<NewOrderItemVo> list1= JSON.parseArray(JSON.toJSONString(list),NewOrderItemVo.class);
//将数据装到list<NewOrderItemVo>
            for (NewOrderItemVo vo :list1
                 ) {


//用skuid去查询出属性
               SkuVo list2 = skuData.listById(vo.getSkuId());
               vo.setSkuVo(list2);

            };

//h获取订单数据
            Order order=service.getById(id);
//装载数据
            all.setList(list1);
            all.setOrder(order);
            lists.add(all);

        };


        return lists;
    }
}
